package com.example.milimili;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.example.milimili.bean.NewsInfo;

import java.util.ArrayList;
import java.util.List;

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    private static final String DB_NAME="myQSLite.db";
    private static final String TABLE_NAME_COLLECT="collect";
    private static final String TABLE_NAME_HISTORY="history";
    private static final String CREATE_TABLE_COLLECT="create table "+TABLE_NAME_COLLECT+" (id integer primary key autoincrement, title text, image text, path text, position integer, content text)";
    private static final String CREATE_TABLE_HISTORY="create table "+TABLE_NAME_HISTORY+" (id integer primary key autoincrement, title text, image text, path text, position integer, content text)";

    public MySQLiteOpenHelper(Context context)
    {
        super(context,DB_NAME,null,1);
    }

    public MySQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE_COLLECT);
        sqLiteDatabase.execSQL(CREATE_TABLE_HISTORY);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public void deleteAll()
    {
        SQLiteDatabase db = getWritableDatabase();
        db.delete(TABLE_NAME_COLLECT, null, null);
        db.delete(TABLE_NAME_HISTORY, null, null);
    }

    public long insertData(int flag,NewsInfo newsInfo)
    {
        if(flag==1)
        {
            if(checkFromDbByTitle(1,newsInfo.getTitle()))
            {
                SQLiteDatabase db = getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("title",newsInfo.getTitle());
                values.put("image",newsInfo.getImage());
                values.put("path",newsInfo.getPath());
                values.put("position",newsInfo.getPosition());
                values.put("content",newsInfo.getContent());
                return db.insert(TABLE_NAME_COLLECT, null, values);
            }
            else
            {
                return -1;
            }
        }
        else
        {
            if(checkFromDbByTitle(2,newsInfo.getTitle()))
            {
                SQLiteDatabase db = getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("title",newsInfo.getTitle());
                values.put("image",newsInfo.getImage());
                values.put("path",newsInfo.getPath());
                values.put("position",newsInfo.getPosition());
                values.put("content",newsInfo.getContent());
                return db.insert(TABLE_NAME_HISTORY, null, values);
            }
           else
            {
                return -1;
            }
        }
    }

    public boolean checkFromDbByTitle(int flag, String title) {
        if(flag==1)
        {
            SQLiteDatabase db = getWritableDatabase();
            Cursor cursor = db.query(TABLE_NAME_COLLECT, null, "title like ?", new String[]{title}, null, null, null);
            if (cursor != null&&cursor.moveToNext()) {
                cursor.close();
                return false;
            }
            else
            {
                return true;
            }
        }
        else
        {
            SQLiteDatabase db = getWritableDatabase();
            Cursor cursor = db.query(TABLE_NAME_HISTORY, null, "title like ?", new String[]{title}, null, null, null);
            if (cursor != null&&cursor.moveToNext()) {
                cursor.close();
                return false;
            }
            else
            {
                return true;
            }
        }
    }

    public List<NewsInfo> queryAllInfo(int flag)
    {
        if(flag==1)
        {
            SQLiteDatabase db = getWritableDatabase();
            List<NewsInfo> newsList = new ArrayList<>();

            Cursor cursor = db.query(TABLE_NAME_COLLECT, null, null, null, null, null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
//
                    String title = cursor.getString((int)cursor.getColumnIndex("title"));
                    String image = cursor.getString((int)cursor.getColumnIndex("image"));
                    String path = cursor.getString((int)cursor.getColumnIndex("path"));
                    int position = cursor.getInt((int)cursor.getColumnIndex("position"));
                    String content=cursor.getString((int)cursor.getColumnIndex("content"));

                    NewsInfo newsInfo = new NewsInfo();
                    newsInfo.setTitle(title);
                    newsInfo.setImage(image);
                    newsInfo.setPath(path);
                    newsInfo.setPosition(position);
                    newsInfo.setContent(content);

                    newsList.add(newsInfo);
                }

                cursor.close();
            }
            return newsList;
        }
        else
        {
            SQLiteDatabase db = getWritableDatabase();
            List<NewsInfo> newsList = new ArrayList<>();

            Cursor cursor = db.query(TABLE_NAME_HISTORY, null, null, null, null, null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
//
                    String title = cursor.getString((int)cursor.getColumnIndex("title"));
                    String image = cursor.getString((int)cursor.getColumnIndex("image"));
                    String path = cursor.getString((int)cursor.getColumnIndex("path"));
                    int position = cursor.getInt((int)cursor.getColumnIndex("position"));
                    String content=cursor.getString((int)cursor.getColumnIndex("content"));

                    NewsInfo newsInfo = new NewsInfo();
                    newsInfo.setTitle(title);
                    newsInfo.setImage(image);
                    newsInfo.setPath(path);
                    newsInfo.setPosition(position);
                    newsInfo.setContent(content);

                    newsList.add(newsInfo);
                }

                cursor.close();
            }
            return newsList;
        }
    }
}
